from jwt import ExpiredSignatureError as _ExpiredSignatureError
from jwt import InvalidTokenError as _InvalidTokenError
from sqlalchemy.exc import IntegrityError as _IntegrityError

# 当需要执行不同操作时，定义新的错误类型
# 当系统复杂后, 自定义 error code, 如 1001,1002

class ErrorMessage:
    NOT_MATCH_OR_USER_DOES_NOT_EXIST = '用户名密码不匹配或者用户不存在'
    TOKEN_MISSED = '无登录令牌，请登录'
    TOKEN_EXPIRED = '登录状态已过期，请重新登录'
    INVALID_TOKEN = '无效的登录信息，请重新登录'
    USER_DOES_NOT_EXIST = '用户不存在'
    # ORDER_STATUS_INVALID = '单据状态无效'
    PERMISSION_DENIED = '权限不足，请联系管理员开通'


class Error(Exception):
    pass


ExpiredSignatureError = _ExpiredSignatureError
InvalidTokenError = _InvalidTokenError
IntegrityError = _IntegrityError


class AuthError(Error):
    def __init__(self, error_msg):
        self.error_msg = error_msg


class BadRequestError(Error):
    def __init__(self, error_msg):
        self.error_msg = error_msg


# ---------- 继承类error，不需要在catch中增加 -------------
class TokenMissedError(AuthError):
    def __init__(self):
        self.error_msg = ErrorMessage.TOKEN_MISSED


class UserDoesNotExistError(AuthError):
    def __init__(self):
        self.error_msg = ErrorMessage.USER_DOES_NOT_EXIST


class NotMatchOrUserDoesNotExistError(BadRequestError):
    def __init__(self):
        self.error_msg = ErrorMessage.NOT_MATCH_OR_USER_DOES_NOT_EXIST


class PermissionDeniedDataAccessException(AuthError):
    def __init__(self):
        self.error_msg = ErrorMessage.PERMISSION_DENIED


class SendSmsFailedException(BadRequestError):
    def __init__(self, error_msg):
        self.error_msg = '发送短信失败: {}'.format(error_msg)
